/*
 * @Author: 杨上漂
 * @Date: 2024-10-14 14:25:29
 * @LastEditors: 杨上漂
 * @LastEditTime: 2024-10-14 16:22:32
 * @FilePath: \gwlc\assets\scripts\SelectSkillPanel.ts
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
import { _decorator, Component, director, instantiate, Node, Prefab, resources } from 'cc';
import { gameMgr } from './gameMgr';
import { selectSkillItem } from './SelectSkillItem';
import { Player } from './Player';
import { randomNum } from '../utils/utils';
import { playerSkillTypeEnum } from '../Enum';
import { UserInfo } from '../startScene/UserInfo';
import { Adsdk } from '../Adsdk';
import { AudioMgr } from '../utils/AudioMgr';
import { StartSceneGameMgr } from '../startScene/StartSceneGameMgr';
const { ccclass, property } = _decorator;

@ccclass('SelectSkillPanel')
export class SelectSkillPanel extends Component {
    
    static ins:SelectSkillPanel;
    skillContent: Node;

    protected onLoad(): void {
        this.skillContent = this.node.getChildByName("bg")
    }
    protected start(): void {
        SelectSkillPanel.ins=this;
    }

    init() {
        this.skillContent.removeAllChildren();

        let displaySkillTypeArr=[];//出现的技能类型数组
        let skillAllTypeArr=[];//所有技能类型数组
        let skillIdArr=[];//最终要呈现的技能id数组

        for (let t = 0; t < gameMgr.ins.playerSkillTypeTotalMun; t++) {
            skillAllTypeArr.push(t);
        }

        while (displaySkillTypeArr.length<3) {
            let type = skillAllTypeArr[randomNum(0, skillAllTypeArr.length - 1)]
            if (Player.ins.playerSkillInfoDic.containsKey(type) == false 
                    && displaySkillTypeArr.indexOf(type) != -1) {//如果技能类型还没解锁，不要重复出现

            }
            else {
                displaySkillTypeArr.push(type);
            }
        }

        let skillCongfi2Arr = [[],[],[],[],[],[],[]]
        gameMgr.ins.skillCongfi.json.forEach(element => {
            skillCongfi2Arr[element.playerSkillType].push(element)
        });

     
        while (skillIdArr.length < 1) {
            let thisSkillCongfi = skillCongfi2Arr[displaySkillTypeArr[0]];
            let randCongfi = thisSkillCongfi[randomNum(0, thisSkillCongfi.length - 1)]

            // randCongfi.effect.hasOwnProperty("isHasEffect")
            if (randCongfi.effect.isHasEffect) {//如果是附加特殊效果描述
                if (this.judgeSkillIsHasAdditionBySkillType(displaySkillTypeArr[0])==true) {//如果已经有附加特殊效果
                    console.log("已经有特殊效果了")
                    continue;
                }
            }
            if (randCongfi.effect.isHasEffectStrengthen) {//如果是有附加特殊效果才能升级的技能（如附加特殊效果增强的技能）
                if (randCongfi.playerSkillType == playerSkillTypeEnum.fireBall) {
                    if (Player.ins.fireBallIsCanFiring == false) {
                        continue;
                    }
                }
                else if (randCongfi.playerSkillType == playerSkillTypeEnum.moonSlash) {
                    if (Player.ins.moonIsCanFrozen == false) {
                        continue;
                    }
                }
            }
            if (skillIdArr.indexOf(randCongfi) == -1) {//一个技能描述不和同一个技能描述重复
                //maxlv条件
                if (Player.ins.playerSkillInfoDic.containsKey(displaySkillTypeArr[0]) == false) {
                    skillIdArr.push(randCongfi)
                }
                else {
                    if (randCongfi.maxlv > Player.ins.playerSkillInfoDetailDic.get(randCongfi.skillId)
                        &&StartSceneGameMgr.ins.sceneGateLevel>=randCongfi.learnlv
                        &&randCongfi.unlearnlv>StartSceneGameMgr.ins.sceneGateLevel) {
                        console.log('detailSillLv1',Player.ins.playerSkillInfoDetailDic.get(randCongfi.skillId))
                        skillIdArr.push(randCongfi)
                    }
                }
            }

          
        }
        while (skillIdArr.length < 2) {
            let thisSkillCongfi = skillCongfi2Arr[displaySkillTypeArr[1]];
            let randCongfi = thisSkillCongfi[randomNum(0, thisSkillCongfi.length - 1)]

            if (randCongfi.effect.isHasEffect) {//如果是附加特殊效果描述
                if (this.judgeSkillIsHasAdditionBySkillType(displaySkillTypeArr[1])==true) {//如果已经有特殊效果
                    console.log("已经有特殊效果了")
                continue;
                }
            }
            if (randCongfi.effect.isHasEffectStrengthen) {//如果是有附加特殊效果才能升级的技能（如附加特殊效果增强的技能）
                if (randCongfi.playerSkillType == playerSkillTypeEnum.fireBall) {
                    if (Player.ins.fireBallIsCanFiring == false) {
                        continue;
                    }
                }
                else if (randCongfi.playerSkillType == playerSkillTypeEnum.moonSlash) {
                    if (Player.ins.moonIsCanFrozen == false) {
                        continue;
                    }
                }
            }
            if (skillIdArr.indexOf(randCongfi) == -1) {//一个技能描述不和同一个技能描述重复
                //maxlv条件
                if (Player.ins.playerSkillInfoDic.containsKey(displaySkillTypeArr[1]) == false) {
                    skillIdArr.push(randCongfi)
                }
                else {
                    if (randCongfi.maxlv > Player.ins.playerSkillInfoDetailDic.get(randCongfi.skillId)
                        &&StartSceneGameMgr.ins.sceneGateLevel>=randCongfi.learnlv
                        &&randCongfi.unlearnlv>StartSceneGameMgr.ins.sceneGateLevel) {
                        console.log('detailSillLv2',Player.ins.playerSkillInfoDetailDic.get(randCongfi.skillId))
                        skillIdArr.push(randCongfi)
                    }
                }
            }
        }
        while (skillIdArr.length < 3) {
            let thisSkillCongfi = skillCongfi2Arr[displaySkillTypeArr[2]];
            let randCongfi = thisSkillCongfi[randomNum(0, thisSkillCongfi.length - 1)]

            if (randCongfi.effect.isHasEffect) {//如果是附加特殊效果描述
                if (this.judgeSkillIsHasAdditionBySkillType(displaySkillTypeArr[2])==true) {//如果已经有特殊效果
                    console.log("已经有特殊效果了")

                continue;
                }
            }
            if (randCongfi.effect.isHasEffectStrengthen) {//如果是有附加特殊效果才能升级的技能（如附加特殊效果增强的技能）
                if (randCongfi.playerSkillType == playerSkillTypeEnum.fireBall) {
                    if (Player.ins.fireBallIsCanFiring == false) {
                        continue;
                    }
                }
                else if (randCongfi.playerSkillType == playerSkillTypeEnum.moonSlash) {
                    if (Player.ins.moonIsCanFrozen == false) {
                        continue;
                    }
                }
            }
            if (skillIdArr.indexOf(randCongfi) == -1) {//一个技能描述不和同一个技能描述重复
                //maxlv条件
                if (Player.ins.playerSkillInfoDic.containsKey(displaySkillTypeArr[2]) == false) {
                    skillIdArr.push(randCongfi)
                }
                else {
                    if (randCongfi.maxlv > Player.ins.playerSkillInfoDetailDic.get(randCongfi.skillId)
                        &&StartSceneGameMgr.ins.sceneGateLevel>=randCongfi.learnlv
                        &&randCongfi.unlearnlv>StartSceneGameMgr.ins.sceneGateLevel) {
                        console.log('detailSillLv3',Player.ins.playerSkillInfoDetailDic.get(randCongfi.skillId))
                        skillIdArr.push(randCongfi)
                    }
                }
            }
        }

        console.log(skillIdArr,"skillIdArr123")

        //生成三个技能item
        for (let i = 0; i < 3; i++) {
            resources.load("prefabs/com/selectSkillItem", Prefab, ((err, res) => {
                let skillItemPre = instantiate(res)
                skillItemPre.setParent(this.skillContent);
                skillItemPre.getComponent(selectSkillItem).init(skillIdArr[i]);
            }))
        }

    }

    //输入技能类型，判断他是否有附加效果
    judgeSkillIsHasAdditionBySkillType(skillType) {
        let res = false;
        switch (skillType) {
            case playerSkillTypeEnum.fireBall:
                res = Player.ins.fireBallIsCanFiring
                break;
            case playerSkillTypeEnum.starLightBullet:
                res = Player.ins.starLightIsCanThrough
                break;
            case playerSkillTypeEnum.moonSlash:
                res = Player.ins.moonIsCanFrozen
                break;
            case playerSkillTypeEnum.light:
                res = Player.ins.lightIsCanscorch
                break;

        }
        return res;
    }

    onclickRefreshSkillBtn() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        Adsdk.instance.showVideo(()=>{
            this.init()
        },()=>{

        })

    }
}


